import time
from datetime import datetime
from binancetrade import binance
import itchat

refresh_time=5 #刷新时间,单位:秒
line='\n'+'-'*100+'\n'
line2='\n'+'='*100+'\n'

MONEY='BTC'
COIN='TNT'
URL='https://api.binance.com/'
plan_name='radical'
money=0.33430834
peak_price=0.00002872
rate=0.000
class trade(object):
    def __init__(self,coin_name,money_name,money,peak_price,plan_name='default'):
        self.coin_name=coin_name
        self.money_name=money_name
        self.money=money#自由金额
        self.lock_money=0#锁定金额
        self.number=0#自由数量
        self.lock_number=0#锁定数量
        self.initial_value=0#初始价值
        self.price=self.get_price()#现价
        self.peak_price=peak_price#最高价
        self.rate=rate#交易费率
        #交易类型
        self.plan_name=plan_name
        self.plan={}
        self.plan['default']={
        'name':'默认方案',
        'benchmark_price':0.85,
        'buy':[
            {'name':'基准价买','price':1,'share':0.25},
            {'name':'跌3点买','price':0.97,'share':0.25},
            {'name':'跌5点买','price':0.95,'share':0.25},
            {'name':'跌10点买','price':0.9,'share':0.25}
            ],

        'sell':[
            {'name':'涨3点卖','price':1,'share':0.25},
            {'name':'涨5点卖','price':1.05,'share':0.25},
            {'name':'涨10点卖','price':1.1,'share':0.25},
            {'name':'涨15点卖','price':1.15,'share':0.25}
            ]

        }

        self.plan['radical']={
        'name':'激进方案',
        'benchmark_price':0.9,
        'buy':[
            {'name':'基准价买','price':1,'share':0.25},
            {'name':'跌3点买','price':0.97,'share':0.375},
            {'name':'跌5点买','price':0.95,'share':0.375}
            ],

        'sell':[
            {'name':'涨5点卖','price':1,'share':0.25},
            {'name':'涨10点卖','price':1.1,'share':0.5},
            {'name':'涨15点卖','price':1.15,'share':0.25}
            ]

        }
            
        #阶梯交易委托数量
        self.commissioned_number={}
     #获取价格
    def get_price(self):
        ba=binance()
        self.price=ba.get_symbol_price(self.coin_name+self.money_name)

    def get_price_info(self):
        self.get_price()
        return '【当前价格】\n[%s]\n%.8f%s/%s\n'%(datetime.now(),self.price,self.money_name,self.coin_name)

    def total_value(self):
        return self.money+self.lock_money+(self.number+self.lock_number)*self.price

    def value_growth_rate(self):
        return (self.total_value()-initial_value)/initial_value

    def get_commissioned_price_info(self):
        info='\n【%s阶梯委托价格参考】\n'%self.plan[plan_name]['name']
        info+='[买入价格]\n'
        for list in self.plan[plan_name]['buy']:
            info+='%s:%.8f\n'%(list['name'],list['price']*self.benchmark_price)
        info+='[卖出价格]\n'
        for list in self.plan[plan_name]['sell']:
            info+='%s:%.8f\n'%(list['name'],list['price']*self.benchmark_price)
        return info

    def get_account_info(self):
        info='\n【账户信息】\n'
        info+='持金额数:%.8f%s\n'%(self.money+self.lock_money,self.money_name)
        info+='持币数:%.8f%s\n'%(self.number+self.lock_number,self.coin_name)
        info+='账户估值:%.8f%s\n'%(self.money+self.lock_money+(self.number+self.lock_number)*self.price,self.money_name)
        info+='增长率:%.2f%%\n'%(self.value_growth_rate()*100)
        return  info


    def commissioned_buy(self,money):#委托买单,money为总钱数
        #print('money:%.8f'%money)#调试用
        plan_name=self.plan_name
        for list in self.plan[plan_name]['buy']:
            p_benchmark_price=self.peak_price*self.plan[plan_name]['benchmark_price']
            p_price=list['price']*p_benchmark_price
            p_money=money*list['share']
            if p_money>p_price:
                p_number=p_money*(1-rate)/p_price
                if not self.commissioned_number.get(list['name']):
                    self.commissioned_number[list['name']]=0
                self.commissioned_number[list['name']]+=p_number
                self.lock_money+=p_money
                self.money-=p_money
                info='【%s】\n'%self.plan[plan_name]['name']
                info+='【委托】[%s]\n%s\n'%(list['name'],datetime.now())
                info+='委托价格:%.8f%s/%s\n'%(p_price,self.coin_name,self.money_name)
                info+='委托数量:%.8f%s\n'%(p_number,self.coin_name)
                info+='委托金额:%.8f%s\n'%(p_money,self.money_name)
                #info+='委托情况:%s'%self.commissioned_number
                print(info)
                itchat.send(info, 'filehelper')#发微信消息

    def commissioned_sell(self,number):#委托卖单,mumber为总币数
        plan_name=self.plan_name
        for list in self.plan[plan_name]['sell']:
            p_benchmark_price=self.peak_price*self.plan[plan_name]['benchmark_price']
            p_price=list['price']*p_benchmark_price
            p_number=number*list['share']
            if p_number>0:
                p_money=p_number*p_price*(1-rate)
                if not self.commissioned_number.get(list['name']):
                    self.commissioned_number[list['name']]=0
                self.commissioned_number[list['name']]+=p_number
                self.lock_number+=p_number
                self.number-=p_number
                info='【%s】\n'%self.plan[plan_name]['name']
                info+='【委托】[%s]\n%s\n'%(list['name'],datetime.now())
                info+='委托价格:%.8f%s/%s\n'%(p_price,self.coin_name,self.money_name)
                info+='委托数量:%.8f%s\n'%(p_number,self.coin_name)
                info+='委托金额:%.8f%s\n'%(p_money,self.money_name)
                #info+='委托情况:%s'%self.commissioned_number
                print(info)
                itchat.send(info, 'filehelper')#发微信消息

    def trade_buy(self):#成交买单
        plan_name=self.plan_name
        self.get_price()
        p_benchmark_price=self.peak_price*self.plan[plan_name]['benchmark_price']
        for list in self.plan[plan_name]['buy']:
            p_price=list['price']*p_benchmark_price
            p_number=self.commissioned_number.get(list['name'],0)
            if self.price<p_price and p_number>0:
                #买单成交
                p_money=p_price*p_number
                self.lock_money-=p_money
                self.number+=p_number
                self.commissioned_number[list['name']]=0

                print(line)
                print(self.get_price_info())
                info='【%s】\n'%self.plan[plan_name]['name']
                info+='【成交】[%s]\n%s\n'%(list['name'],datetime.now())
                info+='成交价格:%.8f%s/%s\n'%(p_price,self.money_name,self.coin_name)
                info+='成交数量:%.8f%s\n'%(p_number,self.coin_name)
                info+='成交金额:%.8f%s\n'%(p_money,self.money_name)
                info+=self.get_account_info()
                #info+='委托情况:%s'%self.commissioned_number
                print(info)
                itchat.send(info, 'filehelper')#发微信消息
                print(self.get_account_info())
                #再挂卖单
                self.commissioned_sell(p_number)


    def trade_sell(self):#成交卖单
        plan_name=self.plan_name
        self.get_price()
        p_benchmark_price=self.peak_price*self.plan[plan_name]['benchmark_price']
        for list in self.plan[plan_name]['sell']:
            p_price=list['price']*p_benchmark_price
            p_number=self.commissioned_number.get(list['name'],0)
            if self.price>p_price and p_number>0:
                #卖单成交
                p_money=p_number*p_price*(1-rate)
                self.money+=p_money
                self.lock_number-=p_number
                self.commissioned_number[list['name']]=0
                print(line)
                print(self.get_price_info())
                info='【%s】\n'%self.plan[plan_name]['name']
                info+='【成交】[%s]\n%s\n'%(pricetype,datetime.now())
                info+='成交价格:%.8f%s/%s\n'%(p_price,self.money_name,self.coin_name)
                info+='成交数量:%.8f%s\n'%(p_number,self.coin_name)
                info+='成交金额:%.8f%s\n'%(p_money,self.money_name)
                info+=self.get_account_info()
                #info+='委托情况:%s'%self.commissioned_number
                print(info)
                itchat.send(info, 'filehelper')#发微信消息
                print(self.get_account_info())


 

#主程序开始执行
def main():
    itchat.auto_login(hotReload=True)#启动微信提示组件

    print(line2)
    info='区块链资产交易辅助系统开始运行'
    print(info)
    itchat.send(info, 'filehelper')#发微信消息
    print(line2)


    t=trade(COIN,MONEY,money,peak_price,plan_name)
    t.benchmark_price=t.peak_price*t.plan[t.plan_name]['benchmark_price']
    t.get_price()
    initial_value=t.total_value()
    min_price=t.price
    info='【基本信息】'
    info+='交易对:%s/%s\n'%(t.coin_name,t.money_name)
    info+='金额:%.8f%s\n'%(t.money,t.money_name)        
    info+='币数:%.8f%s\n'%(t.number,t.coin_name)
    info+='峰值价格:%.8f%s/%s\n'%(t.peak_price,t.money_name,t.money_name)
    info+='基准价格:%.8f%s/%s\n'%(t.benchmark_price,t.money_name,t.money_name)
    info+='交易费率:%.2f%%\n'%(t.rate*100)
    print(info)
    itchat.send(info, 'filehelper')#发微信消息

    #初次买入委托
    print(line)
    print(t.get_account_info())
    info=t.get_price_info()

    t.commissioned_buy(t.money)
    while 1:
        print(info)
        itchat.send(info, 'filehelper')#发微信消息
        for i in range(12*15):
            time.sleep(refresh_time)
            info=t.get_price_info()
            if t.price<min_price:
                min_price=t.price
                up_rate_for_min=(t.price-min_price)/min_price
                info+='【最低价】\n%.8f%s/%s\n'%(min_price,t.money_name,t.coin_name)
                info+='【相对涨幅】\n%.2f%%'%(up_rate_for_min*100)

            #print(info)
            
            if t.price<t.benchmark_price:
                t.trade_buy()
            else:
                t.trade_sell()

if __name__=='__main__':
    main()


        



